package com.android.bookgarden.jpushmsg;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.android.bookgarden.utli.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;


/**
 * 收到极光推送的通知消息
 */
public class NotificationReceiver extends BroadcastReceiver {
    private NotificationManager nm;
    private String Tag = "NotificationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtil.i(Tag, "JPush用户注册成功");


        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.i(Tag, "接受到推送下来的自定义消息");
            String type = judgeType(bundle);
            if (TextUtils.isEmpty(type)) {
                return;
            }
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.i(Tag, "接受到推送下来的通知");
            Log.i(Tag, "onReceive: bundle" + bundle.toString());

            receiveNotification(bundle, context);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.i(Tag, "用户点击打开了通知");

            openNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_CLICK_ACTION.equals(intent.getAction())) {
            LogUtil.i(Tag, "用户点击了通知栏中自定义的按钮。(SDK 3.0.0 以上版本支持)");


        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            LogUtil.i(Tag, "JPush 服务的连接状态发生变化。（注：不是指 Android 系统的网络连接状态。）");

        } else {
            LogUtil.i(Tag, "Unhandled intent - " + intent.getAction());
        }
    }

   


    /**
     * 收到消息后，保存通知消息到本地
     * <p>
     * 01 推广消息
     * 02 语音播报
     * 03 消息反馈
     *
     * @param bundle
     * @param context
     */
    private void receiveNotification(Bundle bundle, Context context) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
       
        
    }

    /**
     * 打开通知
     *
     * @param context
     * @param bundle
     */
    private void openNotification(Context context, Bundle bundle) {
       
        String string = bundle.getString(JPushInterface.EXTRA_EXTRA);
        
            
    }

    
    /**
     * 处理通知栏点开消息
     *
     * @param context
     * @param bundle
     * @param clas
     */
    private void dealNotification(Context context, Bundle bundle, Class<?> clas) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = "";
        try {
            JSONObject extrasJson = new JSONObject(extras);
            myValue = extrasJson.optString("myKey");
        } catch (Exception e) {
            return;
        }

//        if (AppUtils.isAppOnForeground()) {
            //打开自定义的Activity
            Intent i = new Intent(context, clas);
            i.putExtras(bundle);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
            LogUtil.i("NotificationActivity in AppisOnForeground", " start");
//        }
    }


    private String judgeType(Bundle bundle) {
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String type = "";
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(extras);
            type = jsonObject.getString("type");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return type;
    }






}
